/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.model.MakeType;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class MtBusinessDateExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"MtBusinessDate.xls\"");
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(10);
		font.setColor(10);
		font.setFontName("黑体");
		font.setBoldweight(700);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(2);
		cellStyle.setBorderBottom(1);
		cellStyle.setWrapText(true);
		cellStyle.setFillForegroundColor(22);
		cellStyle.setFillPattern(1);
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		c.setCellValue(new HSSFRichTextString("项目合计：" + model.get("busTotal")
				+ "元  优惠合计：" + model.get("favTotal") + "元  实际营业额："
				+ model.get("costTotal") + "元"));
		sheet.addMergedRegion(new Region(0, 0, 0, 2));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("项目类型"));
		r.createCell(1).setCellValue(new HSSFRichTextString("数量（笔）"));
		r.createCell(2).setCellValue(new HSSFRichTextString("营业额"));
		List list = (List) model.get("list");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			r = sheet.createRow(os + 2);
			String preStr = "";
			String mtName = "";
			if (objs[0] == null) {
				mtName = "无项目类型";
			} else {
				mtName = ((MakeType) objs[0]).getTypeName();
			}

			int layer = Integer.parseInt(objs[3].toString());

			for (int j = 0; j < layer - 1; ++j) {
				preStr = preStr + "------";
			}

			r.createCell(0).setCellValue(
					new HSSFRichTextString(preStr + mtName));
			r.createCell(1).setCellValue(
					(new BigDecimal(objs[1].toString())).doubleValue());
			r.createCell(2).setCellValue(
					(new BigDecimal(objs[2].toString())).doubleValue());
		}

		ServletOutputStream arg16 = response.getOutputStream();
		workbook.write(arg16);
		arg16.flush();
		arg16.close();
	}
}